/*
 * @Author: viivLgr 
 * @Date: 2018-03-24 11:44:01 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-26 10:41:42
 */
'use strict';
var _util = require('util/util.js');
var _cities = require('util/cities/index.js');
var _address = require('service/address-service.js');
var templateAddressModal = require('./address-modal.string');


var addressModal = {
    show: function(option){
        this.option = option;
        this.option.data = option.data || {};
        this.$modalWrap = $('.modal-wrap')
        this.loadModal();
        this.bindEvent();
    },
    bindEvent: function(){
        var _this = this;
        this.$modalWrap.find('.modal-container').click(function(e){
            e.stopPropagation();
        });
        // 关闭
        this.$modalWrap.find('.close').click(function(){
            _this.hide();
        });
        // 省份和城市的二级联动
        this.$modalWrap.find('#reveiver-province').change(function(){
            var selectedProvince = $(this).val();
            _this.loadCities(selectedProvince);
        });
        // 提交收货地址
        this.$modalWrap.find('.address-btn').click(function(){
            var receiverInfo = _this.getReveiverInfo(),
                isUpdate = _this.option.isUpdate;
            if(!isUpdate && receiverInfo.status){
                // 添加地址，且地址信息合法
                _address.save(receiverInfo.data, function(res){
                    _util.successTips('地址添加成功');
                    _this.hide();
                    typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
                }, function(err){
                    _util.errorTips(err);
                });
            }else if(isUpdate && receiverInfo.status){
                // 修改地址，且地址信息合法
                _address.update(receiverInfo.data, function(res){
                    _util.successTips('地址修改成功');
                    _this.hide();
                    typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
                }, function(err){
                    _util.errorTips(err);
                });
            }else{
                _util.errorTips(receiverInfo.errMsg || '好像哪里不对了~');
            }
        });
    },
    loadModal: function(){
        var html = _util.renderHtml(templateAddressModal, {
            isUpdate: this.option.isUpdate,
            data: this.option.data
        });
        this.$modalWrap.html(html);
        // 加载省份
        this.loadProvince();
    },
    loadProvince: function(){
        var provinces = _cities.getProvinces() || [],
            $provinceSelecter = this.$modalWrap.find('#reveiver-province');
        $provinceSelecter.html(this.getSelecterOption(provinces));
        // 地址中的省份回填
        var receiverProvince = this.option.data.receiverProvince;
        if(this.option.isUpdate && receiverProvince){
            $provinceSelecter.val(receiverProvince);
            this.loadCities(receiverProvince);
        }
    },
    loadCities: function(province){
        var cities = _cities.getCities(province) || [],
            $citySelecter = this.$modalWrap.find('#reveiver-city');
        $citySelecter.html(this.getSelecterOption(cities));
        // 地址中的城市回填
        var receiverCity = this.option.data.receiverCity;
        if(this.option.isUpdate && receiverCity){
            $citySelecter.val(receiverCity);
        }
    },
    // 获取selecterOption的html,输入array
    getSelecterOption: function(optionArray){
        var html = '<option value="">请选择</option>';
        for(var i = 0, len = optionArray.length; i < len; i++){
            html += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>';
        }
        return html;
    },
    // 获取表单收件人信息，并做表单验证
    getReveiverInfo: function(){
        var receiverInfo = {},
            result = {
                status: false
            };
        receiverInfo.receiverName = $.trim(this.$modalWrap.find('#reveiver-name').val());
        receiverInfo.receiverProvince = this.$modalWrap.find('#reveiver-province').val();
        receiverInfo.receiverCity = this.$modalWrap.find('#reveiver-city').val();
        receiverInfo.receiverAddress = $.trim(this.$modalWrap.find('#reveiver-address').val());
        receiverInfo.receiverMobile = $.trim(this.$modalWrap.find('#reveiver-phone').val());
        receiverInfo.receiverZip = $.trim(this.$modalWrap.find('#reveiver-zip').val());
        if(this.option.isUpdate){
            receiverInfo.id = this.$modalWrap.find('#reveiver-id').val();
        }
        if(!receiverInfo.receiverName){
            result.errMsg = '请输入收件人姓名';
        }else if(!receiverInfo.receiverProvince){
            result.errMsg = '请选择收件人所属省份';
        }else if(!receiverInfo.receiverCity){
            result.errMsg = '请选择收件人所属城市';
        }else if(!receiverInfo.receiverAddress){
            result.errMsg = '请输入收件人详细地址';
        }else if(!receiverInfo.receiverMobile){
            result.errMsg = '请输入收件人手机号';
        }else if(!_util.validate(receiverInfo.receiverMobile, 'phone')){
            result.errMsg = '请输入正确的手机号';
        }else{
            result.status = true;
            result.data = receiverInfo;
        }
        console.log(result)
        return result;
    },
    hide: function(){
        this.$modalWrap.empty();
    }
};

module.exports = addressModal;